package Tydzien6Test;

import static org.junit.Assert.*;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import Tydzien4.Biblioteka;
import Tydzien4.Ksiazka;
import Tydzien6.KsiazkaDao;
import Tydzien6.NullException;
import Tydzien6.WrongClassException;

public class KsiazkaDaoTest {
	
	@Rule
	public ExpectedException exception = ExpectedException.none();

	@Test
	public void testAdd() {
		File file = new File("serialize2");
		KsiazkaDao instance = new KsiazkaDao("serialize2");
		Biblioteka collection = new Biblioteka();
		collection.appendElement(new Ksiazka("Aaa", "AAA", "aaa", 111));
		collection.appendElement(new Ksiazka("Bbb", "BBB", "bbb", 222));
		collection.appendElement(new Ksiazka("Ccc", "CCC", "ccc", 333));
		collection.appendElement(new Ksiazka("Ddd", "DDD", "ddd", 444));
		collection.appendElement(new Ksiazka("Eee", "EEE", "eee", 555));
		instance.add(collection);
		assertEquals(true, file.exists());
		assertEquals(true, file.length() > 0);
	}

	@Test
	public void testGet() throws WrongClassException {
		KsiazkaDao instance = new KsiazkaDao("serialize1");
		
		Biblioteka exact = new Biblioteka();
		exact.appendElement(new Ksiazka("Aaa", "AAA", "aaa", 111));
		exact.appendElement(new Ksiazka("Bbb", "BBB", "bbb", 222));
		exact.appendElement(new Ksiazka("Ccc", "CCC", "ccc", 333));
		exact.appendElement(new Ksiazka("Ddd", "DDD", "ddd", 444));
		exact.appendElement(new Ksiazka("Eee", "EEE", "eee", 555));
		Biblioteka collection = instance.get();
		assertEquals(exact, collection);
	}

	@Test
	public void testWrongClass()  {
		KsiazkaDao instance = new KsiazkaDao("serializeWrongClass");
		
			try { @SuppressWarnings("unused")
			Biblioteka collection = instance.get();
			exception.expect(WrongClassException.class);
			}
			catch (WrongClassException e) {
				Logger.getLogger("ExceptionLog").log(Level.SEVERE, e.getMessage(), e.getCause());
			}
		}


	@Test
	public void testNullException()  {
		
		try { new KsiazkaDao(null);
		exception.expect(NullException.class);
		}
		catch (NullException ne){
			Logger.getLogger("ExceptionLog").log(Level.SEVERE, ne.getMessage(), ne.getCause());
		}
	}
	@Test
	public void testClone () {
		KsiazkaDao instance = new KsiazkaDao("serialize3");
		Biblioteka collection1 = new Biblioteka();
		collection1.appendElement(new Ksiazka("Aaa", "AAA", "aaa", 111));
		collection1.appendElement(new Ksiazka("Bbb", "BBB", "bbb", 222));
		collection1.appendElement(new Ksiazka("Ccc", "CCC", "ccc", 333));
		collection1.appendElement(new Ksiazka("Ddd", "DDD", "ddd", 444));
		collection1.appendElement(new Ksiazka("Eee", "EEE", "eee", 555));
		instance.add(collection1);
		Biblioteka collection2 = collection1.kopiuj();
		assertEquals(collection1, collection2);
		
	}

}